package com.ld.caodan.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.ld.caodan.adapter.CDListAdapter;
import com.ld.caodan.common.Constants;
import com.ld.caodan.entity.Article;
import com.ld.caodan.entity.CaoDanTypeEnum;
import com.ld.caodan.widget.FooterView;
import com.ld.caodan.widget.HeaderView;
import com.ld.caodan.widget.TitleBar;

/**
 * 
 * 
 * @author 刘笛 2011-10-19 下午04:18:51
 */
public abstract class CDListViewBaseActivity extends BasicActivity implements OnTouchListener,OnGestureListener{

	/**
	 * 列表页面中的ListView
	 */
	protected ListView listView;
	/**
	 * 列表中的FooterView
	 */
	protected FooterView footer;

	/**
	 * 临时的Articles 即点击后请求到的新数据
	 */
	protected List<Article> tempArticles = null;
	/**
	 * 全部数据
	 */
	protected List<Article> dateList = new ArrayList<Article>(0);

	/**
	 * 当前页
	 */
	protected int currentIndex = 1;

	/**
	 * Adatper
	 */
	protected CDListAdapter listAdapter = null;

	private TitleBar titleBar;
	
	private GestureDetector mGestureDetector;

	@Override
	public void initContentView() {
		setContentView(R.layout.listview);
	}

	@Override
	protected void initTopTitle() {
		titleBar = (TitleBar) findViewById(R.id.topBar);
		titleBar.getTitle().setText(getType().name());
	}

	@Override
	protected void initView() {
		mGestureDetector = new GestureDetector(this);
		listView = (ListView) findViewById(R.id.listview);
		HeaderView headerView = new HeaderView(this);
//		listView.addHeaderView(new HeaderView(this));
		headerView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "点击Header", Toast.LENGTH_SHORT).show();
			}
		});
		footer = new FooterView(this);
		footer.setText("正在加载...");
		footer.showBar();
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Article article = listAdapter.getItem(position);
				Intent intent = new Intent(CDListViewBaseActivity.this,CD_Article_Info_Activity.class);
				intent.putExtra("info", article);
				startActivity(intent);
			}
		});
		listView.addFooterView(footer);
		listAdapter = new CDListAdapter(this, dateList,listView);
		listView.setAdapter(listAdapter);
//		listView.setOnTouchListener(this);
//		listView.setLongClickable(true);
		loadDate(currentIndex);
	}

	@Override
	protected void initListener() {
		footer.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				footer.setText("正在加载...");
				footer.showBar();
				loadDate(currentIndex);
			}
		});
	}

	/**
	 * 加载数据
	 */
	void loadDate(final int pageNum) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (dateList) {
					// 获取下一页数据
					tempArticles = getType().getCaoDanArtile(pageNum)
							.getListBean();
					if(tempArticles!=null&&tempArticles.size()>0){
						handler.sendEmptyMessage(Constants.SUCC);
					}else{
						handler.sendEmptyMessage(Constants.FAIL);
					}
				}
			}
		}).start();
	}

	/**
	 * 加载该页面数据
	 * 
	 * @param pageNum
	 *            要加载的页码 
	 * @return
	 */
	abstract CaoDanTypeEnum getType();

	Handler handler = new Handler() {
		// 处理下一页数据
		@Override
		public void handleMessage(Message msg) {
			if (Constants.SUCC == msg.what) {
				dateList.addAll(tempArticles); 
				footer.setText("点击加载更多");
				footer.hiddenBar();
				listAdapter.notifyDataSetChanged();
				currentIndex++;
			}
		}
	};

	@Override
	public boolean onDown(MotionEvent e) {
		System.out.println("-----onDown-------");
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		System.out.println("-----onShowPress-------");
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		System.out.println("----onSingleTapUp--------");
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		if(e1.getX() - e2.getX()>200){
			System.out.println("----onScroll-***************----");
		}
		System.out.println("------onScroll------");
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		System.out.println("-----onLongPress-------");
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if(e1.getX() - e2.getX()>200){
			System.out.println("----onFling-***************----");
		}
		System.out.println("------onFling------");
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return mGestureDetector.onTouchEvent(event);
	}
	
	

}
